package com.example.kuv;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class EkDatabaseHelper {
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "kuv.db";
    private static final String TABLE_NAME = "kuvTable";
    public static final String COLUMN_ID = "_id";
    public static final String TAG_MESSAGE = "message";

    KuvHelper openHelper;
    private SQLiteDatabase database;

    private static EkDatabaseHelper databaseHelper;

    public static EkDatabaseHelper getCategoryHelper(Context context) {
        if (databaseHelper == null)
            databaseHelper = new EkDatabaseHelper(context);
        return databaseHelper;
    }

    public EkDatabaseHelper(Context context) throws SQLException {
        openHelper = new KuvHelper(context);
        database = openHelper.getWritableDatabase();
    }

    public void saveKuv(EkData kuv) throws SQLException {
        ContentValues contentValues = new ContentValues();
        contentValues.put(TAG_MESSAGE, kuv.getMessage());
        database.insert(TABLE_NAME, null, contentValues);
        Log.i(EkDatabaseHelper.class.getName(), "Save message to database: "
                + kuv.getMessage() + " ");
    }

    public void reset() throws SQLException {
        openHelper.onUpgrade(database, 0, 0);
    }

    public ArrayList<EkData> getKuvList() throws SQLException {
        Log.i(EkDatabaseHelper.class.getName(), "Get message list ");
        ArrayList<EkData> kuvList = new ArrayList<EkData>();
        // Select All Query

        Cursor cursor = database.rawQuery("select * from " + TABLE_NAME, null);
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                EkData kuv = new EkData();
                kuv.setId(cursor.getInt(0));
                kuv.setMessage(cursor.getString(1));

                Log.i(EkDatabaseHelper.class.getName(),
                        "Get message from database: " + kuv.getId() + " "
                                + kuv.getMessage() + " ");
                // Adding contact to list
                kuvList.add(kuv);
            } while (cursor.moveToNext());
        }
        return kuvList;
    }

    public void deleteKuv(int id) {

        Log.i(EkDatabaseHelper.class.getName(), "Delete kuv " + id + " ");
        database.delete(TABLE_NAME, "_id = ?", new String[] { "" + id });
    }

    private class KuvHelper extends SQLiteOpenHelper {

        public KuvHelper(Context context) {
            // TODO Auto-generated constructor stub
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ("
                    + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + TAG_MESSAGE + " TEXT);");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }
    }
}
